<?php

namespace App\Http\Controllers\Manage;

use App\Model\Config;
use App\Model\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Manage;
use Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Validator;

class ConfigController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        #获取配置信息
        $config = Config::all();
        return view('manage/system/config/index', compact('config'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('manage.system.config.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $input = $request->only('name','rank','title','keyword','description','script');
        $rule = [
            'name' => 'required',
            'rank' => 'required',
            'title' => 'required',
            'keyword' => 'required',
            'description' => 'required',
            'script' => 'required',
        ];
        $message = [
            'required' => ':attribute 不能为空'
        ];
        $attributes = [
            'name' => '网站名称',
            'rank' => '权重',
            'title' => '标题',
            'keyword' => '关键词',
            'description' => '说明',
            'script' => '脚本代码',
        ];
        $validator = Validator::make($input, $rule, $message, $attributes);
        if($validator->fails()) {
            $warnings = $validator->messages()->first();
            return redirect()->back()->withInput()->withErrors($warnings);
        }else{
            $user = session('user.id');
            if(!($user)){
                return Redirect::to('/manage/login');
            }
            #查找重复
            if(Config::where('name',$request['name'])->first()){
                return redirect()->back()->withInput()->withErrors('网站名称已被创建，请勿重复创建！');
            }
            $res = Config::insert([
                'create_user' => $user,
                'name'=>$request->input('name'),
                'rank'=>$request->input('rank'),
                'title'=>$request->input('title'),
                'keyword'=>$request->input('keyword'),
                'description'=>$request->input('description'),
                'script'=>$request->input('script'),
            ]);
            if($res){
                return Redirect::to("manage/config");
            }else{
                return redirect()->back()->withInput()->withErrors('创建失败！');
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        #获取该用户信息
        $config = Config::where('id', $id)->first();
        return view('manage/system/config/edit', compact('config'));

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = $request->only('name', 'rank', 'title', 'keyword', 'description', 'script');
        $rule = [
//            'name' => 'required',
//            'rank' => 'required',
            'title' => 'required',
            'keyword' => 'required',
            'description' => 'required',
//            'script' => 'required',
        ];
        $message = [
            'required' => ':attribute 不能为空'
        ];
        $attributes = [
//            'name' => '网站名称',
//            'rank' => '权重',
            'title' => '标题',
            'keyword' => '关键词',
            'description' => '说明',
//            'script' => '脚本代码',
        ];
        $validator = Validator::make($input, $rule, $message, $attributes);
        if ($validator->fails()) {
            $warnings = $validator->messages()->first();
            return redirect()->back()->withInput()->withErrors($warnings);
        } else {
            $user = session('user.id');
            if(!($user)){
                return Redirect::to('/manage/login');
            }
            if (Config::where('id', $id)->update([
                'update_user' => $user,
//                'name' => $request->input('name'),
//                'rank' => $request->input('rank'),
                'title' => $request->input('title'),
                'keyword' => $request->input('keyword'),
                'description' => $request->input('description'),
//                'script' => $request->input('script'),
            ])){
                return redirect()->back()->withInput()->with('success','更新成功！');
            }else{
                return redirect()->back()->withInput()->withErrors('更新失败！');
            }
        }

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return array
     */
    public function destroy($id)
    {
        $res = Config::where('id',$id)->delete();
        if($res){
            return [
                'code' => 100,
                'msg' => '删除成功！'
            ];
        }else{
            return [
                'code' => 101,
                'msg' => '删除失败！'
            ];
        }
    }
    /**
     * 修改角色状态
     * @return array
     */
    public function setStatus()
    {
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 1) {
            $del = Config::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }else{
            $del = Config::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }
}
